$(function () {
    $("#jqGrid").jqGrid({
        url: 'template/page',
        datatype: "json",
        colModel: [
            { label: 'ID', name: 'id', width: 10, key: true },
            { label: '模板名称', name: 'name', width: 20 },
            { label: '数据源', name: 'ds', width: 20 },
            { label: '包路径', name: 'packageReference', width: 60},
            { label: '模块目录', name: 'moduleName', width: 20 },
            { label: '作者', name: 'author', width: 20},
            { label: '邮箱', name: 'email', width: 30 },
            { label: '表前缀', name: 'tablePrefix', width: 20 },
            { label: '创建时间', name: 'createTime', width: 30 }
        ],
        loadComplete: function (data) {
            if(data.code != 0){
                window.alert(data.msg)
            }
        },
        viewrecords: true,
        height: 385,
        rowNum: 10,
        rowList : [10,30,50,100,200],
        rownumbers: true,
        rownumWidth: 25,
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "data.list",
            page: "data.currPage",
            total: "data.totalPage",
            records: "data.totalCount"
        },
        prmNames : {
            page:"page",
            rows:"limit",
            order: "order"
        },
        gridComplete:function(){
            //隐藏grid底部滚动条
            $("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });
        }
    });
});

var vm = new Vue({
    el:'#rrapp',
    data:{
        q:{
            author: null
        },
        dsList:[
        ],
        ds: '',
        dialogVisible:false,
        addDt:{
            name:null,
            ds:null,
            packageReference:null,
            moduleName:null,
            author:null,
            email:null,
            tablePrefix:null
        }
    },
    created() {
        this.queryDsList()
    },
    mounted(){

    },
    methods: {
        query: function () {
            $("#jqGrid").jqGrid('setGridParam',{
                postData:{'author': vm.q.author,'ds': vm.ds},
                page:1
            }).trigger("reloadGrid");
        },
        queryDsList:function(){
            fetch(localUrl + "/ds/list").then(res => res.json()).then(res => {
                //跨域问题：前端端口8080，后端端口9090，导致跨域
                if(res.code != 0){
                    window.alert(res.msg)
                } else {
                    var dataRList = res.data;
                    vm.dsList = []
                    for(var i = 0; i < dataRList.length; i++){
                        var ii = dataRList[i];
                        vm.dsList.push(ii.ds)
            }
                }
            })
        },
        createDt:function () {
            httpPost(localUrl + "/template/add",vm.addDt,"创建模板配置成功");
            location.reload()
        },
        clear:function () {
            vm.ds='';
            vm.q.author = null
            this.query()
        },
        del:function () {
            var ids = []
            for (const rowData of getSelectedRowsData()) {
                ids.push(parseInt(rowData.id))
            }
            if(ids.length == 0){
                window.alert("请选择需要删除的模板")
            }
            const delParam = {
                "ids":ids
            }
            httpPost(localUrl + "/template/del",delParam,"删除模板配置成功");
            location.reload()
        }
    }
});

